{% extends "main.html" %}
{% block content %}

  <div class="layui-row">
      <div class="layui-col-md12">
        <h1 align="center">当前购买策略</h1>
      </div>    
  </div>
  <div class="layui-row">
      <div class="layui-col-md10">
        &nbsp;
      </div>
      <div class="layui-col-md1">
        <button id="add_strategy_show" class="layui-btn">增加一条策略</button>
      </div>
  </div>
  <hr/>
  <div class="layui-row">
      <div class="layui-col-md12">
        <table class="layui-hide" id="wallet" lay-filter="strategy"></table>
      </div>

    <script type="text/html" id="strategy_status">
      {% raw %}

      {{#  if(d.status == 0){ }}
          <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="info">初始化</a>
      {{#  }else if(d.status == 1){ }}
          <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="info">已启用</a>
      {{#  }else if(d.status == 11){ }}
          <a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="info">已禁用</a>
      {{#  }else if(d.status == 2){ }}
          <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="info">已买入</a>
      {{#  }else if(d.status == 12){ }}
          <a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="info">买入后禁用</a>
      {{#  }else if(d.status == 3){ }}
          <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="info">已卖出</a>
      {{#  }else if(d.status == 4){ }}
          <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="info">已止损</a>
      {{#  }else if(d.status == 5){ }}
          <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="info">人卖出</a>
      {{# }else { }}
          <a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="info">已删除</a>
      {{# } }}

      {% endraw %}
      
    </script>
    <script type="text/html" id="strategy_operation">
      {% raw %}

      {{#  if(d.status == 1 || d.status == 2){ }}
          <a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="edit">禁用</a>
      {{# }else { }}
          <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit">启用</a>
      {{# } }}

      {% endraw %}   
      <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a> 
    </script>
  </div>


    <div id="add_strategy_show_div" style="display: none;">
        <form class="layui-form">
          <div class="layui-form-item">
            <label class="layui-form-label">策略名称</label>
            <div class="layui-input-inline">
              <input type="text" id="strategy_name" required="" lay-verify="required"  autocomplete="off" class="layui-input">
            </div>
          </div>
          <div class="layui-form-item">
            <label class="layui-form-label">GateIo账户</label>
            <div class="layui-input-inline">
              <select id="gateio_id" name="gateio_info" >
                {%for gateio_info in datas.gateio_info_list%}
                <option value="{{gateio_info.id}}">{{gateio_info.name}}</option>
                {%endfor%}
              </select>
            </div>
          </div>
          <div class="layui-form-item">
            <label class="layui-form-label">选择币对</label>
            <div class="layui-input-inline">
              <select id="currency_pair" name="pairs" >
                {%for pair in datas.pairs_list%}
                <option value="{{pair.name}}">{{pair.name}}</option>
                {%endfor%}
              </select>
            </div>
          </div>
          <div class="layui-form-item">
            <label class="layui-form-label">买入价格</label>
            <div class="layui-input-inline">
              <input type="text" id="buy_point" required="" lay-verify="required"  autocomplete="off" class="layui-input">
            </div>
          </div>
          <div class="layui-form-item">
            <label class="layui-form-label">卖出价格</label>
            <div class="layui-input-inline">
              <input type="text" id="sell_point" required="" lay-verify="required"  autocomplete="off" class="layui-input">
            </div>
          </div>


          <div class="layui-form-item">
            <label class="layui-form-label">买入数量</label>
            <div class="layui-input-inline">
              <input type="text" id="filledrate" required="" lay-verify="required"  autocomplete="off" class="layui-input">
            </div>
          </div>

          <div class="layui-form-item">
            <label class="layui-form-label">止损价格</label>
            <div class="layui-input-inline">
              <input type="text" id="stop_point" required="" lay-verify="required"  autocomplete="off" class="layui-input">
            </div>
          </div>
          <div class="layui-form-item">
            <label class="layui-form-label">是否循环</label>
            <div class="layui-input-block">
              <input id="is_loop" type="checkbox" checked="" name="open" lay-skin="switch" lay-filter="switchTest" lay-text="是|否">
            </div>
          </div>
          
        </form>
      </div>



<script type="text/javascript">
  $(document).ready(function() {  
      layui.use('table', function(){
        var table = layui.table
        ,form = layui.form;; 
        table.render({
          elem: '#wallet'
          ,url:'/trade/strategy_info_list'
          ,cellMinWidth: 80 //全局定义常规单元格的最小宽度，layui 2.2.1 新增
          ,page: { //支持传入 laypage 组件的所有参数（某些参数除外，如：jump/elem） - 详见文档
            layout: ['limit', 'count', 'prev', 'page', 'next', 'skip'] //自定义分页布局
            ,curr: 1 //设定初始在第 5 页
            ,groups: 1 //只显示 1 个连续页码
            ,first: false //不显示首页
            ,last: false //不显示尾页
            
          }
          ,cols: [[
            // {type:'numbers'}
            // ,{type: 'checkbox'}
            {field:'id', width:120, title: 'ID', sort: true}
            ,{field:'name', width:120, title: '策略名称'}
            ,{field:'gateio', width:120, title: 'gate账户'}
            ,{field:'currency_pair', width:120, title: '币值对'}
            ,{field:'buy_point', width:120, title: '买点', sort: true}
            ,{field:'sell_point', width:120, title: '卖点', sort: true}
            ,{field:'stop_point', width:120, title: '止损点', sort: true}
            ,{field:'filledrate', width:120, title: '买入量', sort: true}
            ,{field:'fee_total', width:120, title: '每币盈利', sort: true}
            ,{field:'is_loop', width:120, title: '是否循环', sort: true}
            ,{field:'status', width:80, title: '状态', toolbar: '#strategy_status'}
            ,{field:'operation', title:'操作', width:120, toolbar: '#strategy_operation'}
            ,{field:'ctime', title: '创建时间', sort: true}
            ,{field:'utime', title: '更新时间', sort: true}
            // ,{field:'status', title:'性别', width:85, templet: '#switchTpl', unresize: true}
          ]]
        });

        table.on('tool(strategy)', function(obj){ //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            var tr = obj.tr; //获得当前行 tr 的DOM对象
           
            if(layEvent === 'detail'){ //查看
              //do somehing
            } else if(layEvent === 'del'){ //删除
                layer.confirm('真的删除行么', function(index){
                  //向服务端发送删除指令
                  oid = data.id;
                  $.ajax({
                      url: "/trade/del_strategy",
                      type: "POST",
                      dataType: "json",
                      data:{"oid":oid},
                      success: function (result){
                          if(result['datas']['code']=="1"){
                              layer.msg(result['datas']['msg']);
                              obj.del(); //删除对应行（tr）的DOM结构，并更新缓存
                          }else{
                              layer.msg(result['datas']['msg']);
                          }
                      }
                  });
                  layer.close(index);
              });
            } else if(layEvent === 'edit'){ //编辑
                oid = data.id;
                $.ajax({
                    url: "/trade/update_strategy",
                    type: "POST",
                    dataType: "json",
                    data:{"oid":oid},
                    success: function (result){
                        if(result['datas']['code']=="1"){
                            layer.msg(result['msg']);
                            location.href=location.href;
                        }else{
                            layer.msg(result['msg']);
                        }
                    }
                });
                

            }
        });

      });

      //------------------------------------------
      layui.use('layer', function(){ //独立版的layer无需执行这一句
        var $ = layui.jquery, layer = layui.layer; //独立版的layer无需执行这一句
        //触发事件
      });
      $("#add_strategy_show").click(function(){
          layer.open({
              type: 1
              ,title: ['增加一条新策略', 'font-size:18px;align:center;']
              ,closeBtn: false
              ,area: ['400px', '550px']
              ,shade: 0.8
              ,id: 'LAY_layuipro' //设定一个id，防止重复弹出
              ,btn: ['保存', '取消']
              ,btnAlign: 'c'
              ,moveType: 1 //拖拽模式，0或者1
              ,content: $('#add_strategy_show_div')
              ,success: function(layero){
                var btn = layero.find('.layui-layer-btn');
                btn.find('.layui-layer-btn0').click(function(){
                  var strategy_name = $("#strategy_name").val();
                  var oid = $("#gateio_id").val();
                  var currency_pair = $("#currency_pair").val();
                  var buy_point = $("#buy_point").val();
                  var sell_point = $("#sell_point").val();
                  var stop_point = $("#stop_point").val();
                  var filledrate = $("#filledrate").val();
                  var is_loop_status = $("#is_loop").val();
                  if(is_loop_status=="on"){
                    var is_loop=1;
                  }else{
                    var is_loop=0;
                  }
                  $.ajax({
                      url: "/trade/add_strategy",
                      type: "POST",
                      dataType: "json",
                      data:{"strategy_name":strategy_name,"oid":oid,"currency_pair":currency_pair,"buy_point":buy_point,"sell_point":sell_point,"stop_point":stop_point,"filledrate":filledrate,"is_loop":is_loop},
                      success: function (result){
                          if(result['datas']['code']=="1"){
                              layer.msg(result['msg']);
                              location.href=location.href;
                          }else{
                              layer.msg(result['msg']);
                          }
                      }
                  });
                });

              }
            });
      });


  });


</script>





{% endblock %}